<?php
/*! \brief
 * This is WFString class
 * Manipulate all the string handle
 * \author William Su
 */
class WFString
{
    /**
     *\brief Description
     * - get upper camel case
     * - EX : widget_id = WidgetId
     * - EX : WIDGET_ID = WidgetId
     * @return string
     */
    public function getUpperCamelCase($str){
        $str = strtolower($str);
        $isUnderscore = false;
        if(false !== strpos($str, '_')){
            $str = str_replace('_', ' ', $str);
            $isUnderscore = true;
        }
        $str = strtolower($str);
        $str = ucwords($str);
        if(true === $isUnderscore){
            $str = str_replace(' ', '', $str);
        }
        return $str;
    }
    /**
     *\brief Description
     * - check is version number is float
     * - EX : 10.5.1001 = false
     * - EX : 10.5 = true
     * @return boolean
     */
    public function isVersionFloat($mix){
        $numberDot = substr_count($mix, '.');
        $ret = ($numberDot < 2) ? true : false;
        return $ret;
    }
    /**
     *\brief Description
     * - get float version number by string version number
     * - usage : php /path/to/interface_request.php turnOnWidgetOfCategoryByVersion.php Category2 10.5.1001
     * - will execute turnOn Widget WHERE widget_category='Category2' AND min_support_version_float <= 1000005.1001
     * -Format of version:
     * Max length of  MajorVersion   = 10 digit
     * Max length of  MinorVersion  = 5 digit
     * Max length of  BuildVersion   = 5 digit
     * - EX : 10.5.1001 = 10.0000501001
     * - EX : 0 = 0
     * - EX : null = 0
     * - EX : '' = 0
     * @return float
     */
    public function getVersionFloatByString($strVersionNumber){
        if(false === $this->isVersionNumberValid($strVersionNumber)){
            $strMsg = __CLASS__ . '::' . __FUNCTION__ . '()=>Parameter $strVersionNumber is not valid!';
            throw new WFTypeStringException($strMsg);
        }
        if(! $strVersionNumber){ //if version is empty, version_float = 0;
            return 0;
        }
        $arrVersion = explode('.', $strVersionNumber);
        $strVersion = isset($arrVersion[0]) ? $arrVersion[0] : null;
        $strMinorVersion = isset($arrVersion[1]) ? $arrVersion[1] : null;
        $strBuildVersion = isset($arrVersion[2]) ? $arrVersion[2] : null;
        $strMinorVersion = null !== $strMinorVersion ? sprintf('%-05s', $strMinorVersion) : '00000';
        $strBuildVersion = null !== $strBuildVersion ? sprintf('%-05s', $strBuildVersion) : '00000';
        $ret = $strVersion . '.' . $strMinorVersion . $strBuildVersion;
        return (float) abs($ret);
    }
    /**
     *\brief Description
     * - get float version number by string version number
     * - usage : php /path/to/interface_request.php turnOnWidgetOfCategoryByVersion.php Category2 10.5.1001
     * - will execute turnOn Widget WHERE widget_category='Category2' AND min_support_version_float <= 1000005.1001
     * - EX : 10.5.1001 = 1000005.1001
     * @return float
     */
    /**
     *\brief Description
     * - var_dump result to $variable
     * @return string $variable
     */
    private function getVarDump($mixed = null){
        ob_start();
        var_dump($mixed);
        $content = ob_get_contents();
        ob_end_clean();
        return $content;
    }
    private static $instance;
    /**
     *\brief Description
     * - pretty var_dump $variable
     * - Debug useful method
     * @return string
     */
    public function getVarLog(&$varInput, $var_name = '', $reference = '', $method = '=', $sub = false){
        if($sub == false){
            self::$instance['output'] = '';
            self::$instance['depth'] = 0;
            $reference = $var_name;
            $var = serialize($varInput);
            $var = unserialize($var);
        }else{
            ++self::$instance['depth'];
            $var = & $varInput;
        }
        // constants
        $nl = "\n";
        $block = 'a_big_recursion_protection_block';
        $c = self::$instance['depth'];
        $indent = '';
        while($c-- > 0){
            $indent .= '|  ';
        }
        // if this has been parsed before
        if(is_array($var) && isset($var[$block])){
            $real = & $var[$block];
            $name = & $var['name'];
            $type = gettype($real);
            self::$instance['output'] .= $indent . $var_name . ' ' . $method . '& ' . ($type == 'array' ? 'Array' : get_class($real)) . ' ' . $name . $nl;
             // havent parsed this before
        }else{
            // insert recursion blocker
            $var = Array($block => $var, 'name' => $reference);
            $theVar = & $var[$block];
            // print it out
            $type = gettype($theVar);
            switch($type){
                case 'array':
                    self::$instance['output'] .= $indent . $var_name . ' ' . $method . ' Array (' . $nl;
                    $keys = array_keys($theVar);
                    foreach($keys as $name){
                        $value = &$theVar[$name];
                        $this->getVarLog($value, $name, $reference . '["' . $name . '"]', '=', true);
                    }
                    self::$instance['output'] .= $indent . ')' . $nl;
                    break;
                case 'object':
                    self::$instance['output'] .= $indent . $var_name . ' = ' . get_class($theVar) . ' {' . $nl;
                    foreach($theVar as $name => $value){
                        $this->getVarLog($value, $name, $reference . '->' . $name, '->', true);
                    }
                    self::$instance['output'] .= $indent . '}' . $nl;
                    break;
                case 'string':
                    self::$instance['output'] .= $indent . $var_name . ' ' . $method . ' "' . $theVar . '"' . $nl;
                    break;
                default:
                    self::$instance['output'] .= $indent . $var_name . ' ' . $method . ' (' . $type . ') ' . $theVar . $nl;
                    break;
            }
             // $var=$var[$block];
        }
        --self::$instance['depth'];
        if($sub == false)
            return self::$instance['output'];
    }
    /**
     *\brief Description
     * - Check if string is hash by md5
     * @param string $var
     */
    public function isMd5($var){
        $ret = preg_match('/^[A-Fa-f0-9]{32}$/', $var);
        return $ret ? true : false;
    }
    /**
     *\brief Description
     * - get boolean string
     * - EX : "" => 'false'
     * - EX : 1 => 'true'
     * - EX : 0 => 'false'
     * - EX : null => 'false'
     * @return string return 'true' if true, return 'false' if false
     */
    public function getBooleanString($bool){
        $ret = ! $bool ? 'false' : 'true';
        return $ret;
    }
    /**
     *\brief Description
     * - get null string
     * - EX : "" => 'null'
     * - EX : 1 => 1
     * - EX : 'somestring'=>'somestring'
     * - EX : 0 => 'null'
     * - EX : null => 'null'
     * return 'null' if null/empty, else return value of variable
     */
    public function getNullString($mix){
        $ret = ! $mix ? 'null' : $mix;
        return $ret;
    }
    /**
     *\brief Description
     * - Check is version number valid
     * - EX : 2.A => false(not numberic)
     * - EX : 1234567899.12345.12345 => true (major=10digits,minor=5digits,build=5digits)
     * - EX : 12345678999.123456.12345 => false(build is exceed maximun digits)
     * - EX : 1234567899.123456.12345 => false(minor is exceed maximun digits)
     * - EX : 1234567899.12345.123456 => false(build is exceed maximun digits)
     * @param boolean return true is valid, return false is false
     */
    public function isVersionNumberValid($strVersionNumber){
        $ret = true;
        if(false !== strpos($strVersionNumber, '.')){
            $arrVersion = explode('.', $strVersionNumber);
            foreach($arrVersion as $strVersion){
                if(false === is_numeric($strVersion)){
                    $ret = false;
                }
            }
            $arrVersion = explode('.', $strVersionNumber);
            $strVersion = isset($arrVersion[0]) ? $arrVersion[0] : null;
            $strMinorVersion = isset($arrVersion[1]) ? $arrVersion[1] : null;
            $strBuildVersion = isset($arrVersion[2]) ? $arrVersion[2] : null;
            if(null !== $strVersion && strlen($strVersion) > 10){
                $ret = false;
            }
            if(null !== $strMinorVersion && strlen($strMinorVersion) > 5){
                $ret = false;
            }
            if(null !== $strBuildVersion && strlen($strBuildVersion) > 5){
                $ret = false;
            }
        }else{
            if($strVersionNumber && false === is_numeric($strVersionNumber)){
                $ret = false;
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * This method will replace new line symbol (\r,\r\n,\n) to empty
     */
    public function replaceNewline($string){
        return (string) str_replace(array("\r", "\r\n", "\n"), '', $string);
    }
}